from src.gpio import GPIOController
import RPi.GPIO as GPIO
from src.log import log
import paho.mqtt.client as mqtt
from src.ResStatus import ResStatus

class LEDController:
    def __init__(self, pin, config):
        self.config = config
        self.pin = pin
        self.gpio = GPIOController(self.config['GPIO']['mode'])
        self.gpio.setup_pin(self.pin, GPIO.OUT)
        self.log = log
        

    def on(self):
        self.gpio.output_pin(self.pin, GPIO.HIGH)

    def off(self):
        self.gpio.output_pin(self.pin, GPIO.LOW)


# mqtt 回调函数
def LedFunc(client, userdata, msg):
    led = userdata['led']
    led.log.info(f"接收的消息： {msg.payload.decode()}")
    if msg.payload.decode() == led.config['payload']['led']['onPL']:
        led.on()
        led.log.info(ResStatus.LED_ON_SUCCESS)
        client.publish(led.config['topic']['lednews'], ResStatus.LED_ON_SUCCESS.message)

    elif msg.payload.decode() == led.config['payload']['led']['offPL']:
        led.off()
        led.log.info(ResStatus.LED_OFF_SUCCESS)
        client.publish(led.config['topic']['lednews'], ResStatus.LED_OFF_SUCCESS.message)



      